package com.rp.common.utils;

import java.io.IOException;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

/**
 * Properties配置文件工具
 * @author HuangJian
 *
 */
public class PropertiesLoader {
	
	
	public static final String SUPERADMIN_AUTHCODE = "superAdminAuthCode";
	public static final String PROPERTIES_NAME = "rp.properties";

	/**
	 * 加载配置文件并返回Properties实例
	 * @param filePath 默认读取classpath路径下的配置文件
	 * @return
	 */
	public static Properties loadProperties(String filePath){
		Resource resource = new ClassPathResource(filePath);
		Properties props = null;
		try {
			props = PropertiesLoaderUtils.loadProperties(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return props;
	}
	
	/**
	 * 
	 * @param key
	 * @param propertiesName 如果传入NULL，则读取平台默认的配置文件（rp.properties）
	 * @return
	 */
	public static String getValue(String key, String propertiesName){
		Properties properties = null;
		if(propertiesName == null){
			properties = PropertiesLoader.loadProperties(PROPERTIES_NAME);
		}else{
			properties = PropertiesLoader.loadProperties(propertiesName);
		}
		return properties.getProperty(key, "");
	}
	
}
